package by.ostrovsky.webhousingsystem.controller.validation;

import by.ostrovsky.webhousingsystem.bean.user.RegistrationAction;
import by.ostrovsky.webhousingsystem.controller.ICheckController;
import by.ostrovsky.webhousingsystem.exception.CommonException;

public class RegistrationValidatorController implements
		ICheckController<RegistrationAction, Boolean> {

	@Override
	public Boolean check(RegistrationAction form) throws CommonException {
		boolean isValid = false;

		try {
			isValid = !form.getLogin().isEmpty();
			isValid = isValid && form.getPassword1().length() >= 8
					&& !form.getPassword1().isEmpty();
			isValid = isValid
					&& form.getPassword2().equals(form.getPassword1());
			isValid = isValid && !form.getUsername().isEmpty();
			isValid = isValid && !form.getMail().isEmpty();
		} catch (NullPointerException e) {
			return false;
		}
		// user will not see in what field(s) he makes a mistake(s)
		// JavaScript must be on
		return isValid;
	}
}
